package com.travel.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.dto.UserInfoDTO;
import com.travel.mapper.ScenicSpotMapper;
import com.travel.mapper.UserMapper;
import com.travel.mapper.UserOrderMapper;
import com.travel.pojo.OrderData;
import com.travel.pojo.ScenicSpot;
import com.travel.pojo.User;
import com.travel.pojo.UserOrder;
import com.travel.service.UserOrderService;
import com.travel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: TravelDream
 * @BelongsPackage: com.travel.service.impl
 * @CreateTime: 2021-05-21 14:37
 * @Description: TODO
 */
@Service
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder> implements UserOrderService {

    @Resource
    UserOrderMapper userOrderMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    @Override
    public List<OrderData> getOrderDatas() {
        return userOrderMapper.getOrderDatas();
    }

    @Override
    public UserInfoDTO getUserInfo(String orderId) {
        UserOrder userOrder = userOrderMapper.selectById(orderId);
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        if (userOrder != null) {
            List<UserInfoDTO.Participant> plist = new ArrayList<>();
            UserInfoDTO.Participant participant = new UserInfoDTO.Participant();
            participant.setLabel("成人");
            participant.setPrice(userOrder.getAdultPrice());
            participant.setCount(String.valueOf(userOrder.getAdultCount()));
            plist.add(participant);

            participant = new UserInfoDTO.Participant();
            participant.setLabel("老人学生");
            participant.setPrice(userOrder.getChildrenPrice());
            participant.setCount(String.valueOf(userOrder.getChildrenCount()));
            plist.add(participant);

            participant = new UserInfoDTO.Participant();
            participant.setLabel("儿童(一米三一下)");
            participant.setPrice(userOrder.getChildrenPrice());
            participant.setCount(String.valueOf(userOrder.getChildrenCount()));
            plist.add(participant);
            userInfoDTO.setParticipant(plist);

            // 假设getProductId()返回的是整数字符串，且多个整数以逗号分隔
            String productIdString = userOrder.getProductId();
            if(productIdString != null && !productIdString.isEmpty()) {
                String[] productIdArray = productIdString.split(",");
                List<String> itemIds = Arrays.asList(productIdArray);
                userInfoDTO.setSelectedRoutes(itemIds);
            }

            userInfoDTO.setTotal((int) userOrder.getFee());

            userInfoDTO.setName(userOrder.getUserName());
            userInfoDTO.setPhone(userOrder.getLinkTel());
            userInfoDTO.setBackupContact(userOrder.getLinkTelBk());
            userInfoDTO.setHotelAddress(userOrder.getHotelAddress());
        }

        User user = userMapper.selectById(userOrder.getUserId());
        if (user != null) {
            userInfoDTO.setUserId(userOrder.getUserId());
        }
        return userInfoDTO;
    }

    @Override
    public void updateUserInfo(UserInfoDTO userInfoDTO, String orderId) {
        // 更新订单信息
        UserOrder userOrder = userOrderMapper.selectById(orderId);
        // 设置各类型人数
        List<UserInfoDTO.Participant> plist = userInfoDTO.getParticipant();
        int peopleCount = 0;
        for (UserInfoDTO.Participant item : plist) {
            if (item.getLabel().equals("成人")){
                userOrder.setAdultCount(Integer.valueOf(item.getCount()));
                userOrder.setAdultPrice(item.getPrice());
                peopleCount += Integer.parseInt(item.getCount());
            }
            if (item.getLabel().equals("老人学生")){
                userOrder.setOldStuCount(Integer.valueOf(item.getCount()));
                userOrder.setOldStuPrice(item.getPrice());
                peopleCount += Integer.parseInt(item.getCount());
            }
            if (item.getLabel().startsWith("儿童")){
                userOrder.setChildrenCount(Integer.valueOf(item.getCount()));
                userOrder.setChildrenPrice(item.getPrice());
                peopleCount += Integer.parseInt(item.getCount());
            }
        }
        // 设置人数
        userOrder.setPeopleCount((long) peopleCount);
        // 设置总价
        userOrder.setFee(userInfoDTO.getTotal());
        // 插入旅游项目id
        List<String> selectedRoutes = userInfoDTO.getSelectedRoutes();
        String ids = CollUtil.join(selectedRoutes, ",");
        // 获取产品名称
        List<ScenicSpot> scenicSpots = scenicSpotMapper.selectBatchIds(selectedRoutes);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < scenicSpots.size(); i++) {
            if (i == scenicSpots.size() - 1) {
                stringBuilder.append(scenicSpots.get(i).getSpotName());
                break;
            }
            stringBuilder.append(scenicSpots.get(i).getSpotName() + ";");
        }
        userOrder.setProductName(stringBuilder.toString());
        // 设置订单姓名
        userOrder.setUserName(userInfoDTO.getName());
        // 设置联系电话
        userOrder.setLinkTel(userInfoDTO.getPhone());
        // 设置备用联系电话
        userOrder.setLinkTelBk(userInfoDTO.getBackupContact());
        // 设置酒店地址
        userOrder.setHotelAddress(userInfoDTO.getHotelAddress());
        userOrder.setProductId(ids);
        userOrderMapper.updateById(userOrder);
    }
}
